package main

//go:generate gotext -srclang=en update -out=catalog/catalog.go -lang=en,zh

import (
	"fmt"

	"golang.org/x/text/feature/plural"
	"golang.org/x/text/language"
	"golang.org/x/text/message"
	"golang.org/x/text/message/catalog"
)

// 翻译条目
type entry struct {
	tag language.Tag
	key string
	msg interface{}
}

var entries = [...]entry{
	{language.English, "110", "Hello World"},
	{language.Chinese, "110", "你好世界!"},
	{language.English, "%d task(s) remaining!", plural.Selectf(1, "%d",
		"=1", "One task remaining!",
		"=2", "Two tasks remaining!",
		"other", "[1]d tasks remaining!",
	)},
	{language.Chinese, "%d task(s) remaining!", plural.Selectf(1, "%d",
		"=1", "剩余一项任务！",
		"=2", "剩余两项任务！",
		"other", "剩余 [1]d 项任务！",
	)},
}

var matcher = language.NewMatcher(message.DefaultCatalog.Languages())

func init() {
	for _, e := range entries {
		// tag := language.MustParse(e.tag)
		switch msg := e.msg.(type) {
		case string:
			message.SetString(e.tag, e.key, msg)
		case catalog.Message:
			message.Set(e.tag, e.key, msg)
		case []catalog.Message:
			message.Set(e.tag, e.key, msg...)
		}
	}
}

func main() {
	tg := language.MustParse("en")
	p := message.NewPrinter(tg)
	p.Printf("110")
	p.Println()
	p.Printf("%d task(s) remaining!", 2)
	p.Println()

	p = message.NewPrinter(language.English)
	p.Printf("110")
	p.Println()
	p.Printf("%d task(s) remaining!", 2)
	p.Println()
	fmt.Println("---------")

	tag, _, _ := matcher.Match(language.Chinese)
	p = message.NewPrinter(tag)
	// // p.Printf("110 \n")
	p.Printf("%d task(s) remaining!", 2)
	// fmt.Println("----------")
}
